<template>
  <div>
    <!-- 搜索框 -->
    <el-form :inline="true" :model="searchEmployee" class="demo-form-inline">
      <el-form-item label="员工账号">
        <el-input v-model="searchEmployee.account" placeholder="员工账号" clearable />
      </el-form-item>

      <el-form-item label="员工昵称">
        <el-input v-model="searchEmployee.nickname" placeholder="员工昵称" clearable />
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="searchEmployee.telephone" placeholder="手机号" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="selectEmployee">搜索</el-button>
        <el-button type="info" @click="openAddDialog">添加员工</el-button>

      </el-form-item>
    </el-form>

    <!-- 表格标签 -->
    <el-table :data="emplyeeData" style="width: 100%" border stripe>
      <el-table-column type="index" width="100" label="序号" align="center" />
      <el-table-column prop="account" label="账号" align="center" />
      <el-table-column prop="nickname" label="昵称" align="center" />
      <el-table-column prop="telephone" label="手机号" align="center" />
      <el-table-column prop="role" label="角色" align="center" />
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <span v-if="scope.row.is_del == 0">正常</span>
          <span v-else-if="scope.row.is_del == 1">禁用</span>
          <span v-else>离职</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" @click="openEditDialog(scope.row)">修改</el-button>
          <el-button type="danger" @click="delEmployee(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <!-- 分页工具条 -->
    <el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="[5, 10, 20, 50]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />

    <!-- 新增对话框 -->
    <!-- <el-dialog v-model="dialogFormVisible" title="新增员工" width="500">
      <el-form :model="employee">
        <el-form-item label="账号">
          <el-input v-model="employee.account" autocomplete="off" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item label="密码">
          <el-input v-model="employee.password" autocomplete="off" type="password" clearable />
        </el-form-item>

        <el-form-item label="昵称">
          <el-input v-model="employee.nickname" autocomplete="off" />
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="employee.telephone" autocomplete="off" />
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addEmployee">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog> -->

    <!-- 修改/新增对话框 -->
    <el-dialog v-model="dialogFormVisibleEdit" :title="title" width="500">
      <el-form :model="emplyeeEdit">
        <el-form-item label="账号">
          <el-input v-model="emplyeeEdit.account" autocomplete="off" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item label="密码">
          <el-input v-model="emplyeeEdit.password" autocomplete="off" type="password" clearable />
        </el-form-item>

        <el-form-item label="昵称">
          <el-input v-model="emplyeeEdit.nickname" autocomplete="off" />
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="emplyeeEdit.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="emplyeeEdit.role" placeholder="Select" style="width: 240px">
            <el-option value="admin">管理员</el-option>
            <el-option value="manager">经理</el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" v-if="title == '修改员工'">
          <el-select v-model="emplyeeEdit.is_del" placeholder="Select" style="width: 240px">
            <el-option label="正常" :value="0"/>
            <el-option label="禁用" :value="1"/>
            <el-option label="离职" :value="2"/>
          </el-select>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
          <el-button type="primary" @click="editOrAddEmployee">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ElMessageBox, ElMessage } from 'element-plus'
import qs from 'qs'
export default {
  data() {
    return {
      emplyeeData: [],
      // dialogFormVisible: false,
      employee: {},
      dialogFormVisibleEdit: false,
      emplyeeEdit: {},
      title: '',
      searchEmployee: {},
      currentPage: 1,
      pageSize: 10,
      total: 400
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.selectEmployee();
    },
    handleCurrentChange(val) {
      this.currentPage=val;
      this.selectEmployee();
    },
    editOrAddEmployee() {

      if (this.title == '新增员工') {
        this.addEmployee();
      } else {
        //发起异步请求,完成修改功能
        //1.创建ajax对象
        let xmlhttp = new XMLHttpRequest();

        xmlhttp.onreadystatechange = () => {
          // 当状态为4并且响应状态码为200,代表响应成功
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (xmlhttp.responseText == 1) {

              //0.清除数据
              this.employeeEdit = {}
              this.dialogFormVisibleEdit = false;
              //1.重新查询
              this.selectEmployee();
              ElMessage({
                type: 'success',
                message: '修改成功',
              })
            }
          }
        }
        //3.请求地址和请求方式
        xmlhttp.open("POST", "http://localhost:8080/updateEmployee", true);
        //4.发送请求
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        // qs工具类拼接参数
        xmlhttp.send(qs.stringify(this.emplyeeEdit));
      }


    },
    openAddDialog() {
      this.dialogFormVisibleEdit = true;
      this.title = '新增员工';
      this.emplyeeEdit = {}
    },
    openEditDialog(row) {
      this.title = '修改员工'
      this.dialogFormVisibleEdit = true;
      this.emplyeeEdit = JSON.parse(JSON.stringify(row));
    },
    addEmployee() {
      //发起异步请求,完成新增功能
      //1.创建ajax对象
      let xmlhttp = new XMLHttpRequest();

      xmlhttp.onreadystatechange = () => {
        // 当状态为4并且响应状态码为200,代表响应成功
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          if (xmlhttp.responseText == 1) {

            //0.清除数据
            this.emplyeeEdit = {}
            this.dialogFormVisibleEdit = false;
            //1.重新查询
            this.selectEmployee();
            ElMessage({
              type: 'success',
              message: '新增成功',
            })
          }
        }
      }
      //3.请求地址和请求方式
      xmlhttp.open("POST", "http://localhost:8080/addEmployee", true);
      //4.发送请求
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.send(qs.stringify(this.emplyeeEdit));
    },
    selectEmployee() {
      //1.创建ajax对象
      let xmlhttp = new XMLHttpRequest();

      xmlhttp.onreadystatechange = () => {
        // 当状态为4并且响应状态码为200,代表响应成功
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          this.emplyeeData.splice(0, this.emplyeeData.length);

          let pageInfo = JSON.parse(xmlhttp.responseText);
          this.emplyeeData.push(...pageInfo.list);
          this.total= pageInfo.total;
        }
      }
      //3.请求地址和请求方式
      xmlhttp.open("GET", "http://localhost:8080/employee?page="+this.currentPage+"&pageSize="+this.pageSize+"&"+qs.stringify(this.searchEmployee), true);
      //4.发送请求
      xmlhttp.send();
    },
    delEmployee(row) {
      ElMessageBox.confirm(
        '您确定要删除该员工吗?',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        //发起异步请求,完成删除功能
        //1.创建ajax对象
        let xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = () => {
          // 当状态为4并且响应状态码为200,代表响应成功
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (xmlhttp.responseText == 1) {
              //1.重新查询
              this.selectEmployee();

              ElMessage({
                type: 'success',
                message: '删除成功',
              })
            }
          }
        }
        //3.请求地址和请求方式
        xmlhttp.open("GET", "http://localhost:8080/deleteEmployee?id=" + row.id, true);
        //4.发送请求
        xmlhttp.send();
      }).catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
    }
  },

  created() {
    this.selectEmployee();
  }
}
</script>

<style scoped></style>